#include "viewwidget.h"
#include <QLayout>

ViewWidget::ViewWidget(QWidget *parent, PageWidget *page, IPluginGadget * pluginconfig) :
    QWidget(parent),
    m_pagewidget(page),
    m_plugingadget(pluginconfig),
    m_layout(new QVBoxLayout(this)),
    m_uavGadgetList(new QComboBox(this)),
    m_closeButton(new QPushButton(this)),
    isActive(false)
{
    m_toolbarLayout = new QHBoxLayout();
    QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    m_closeButton->setText("Close");
    m_toolbarLayout->addWidget(m_uavGadgetList);
    m_toolbarLayout->addSpacerItem(horizontalSpacer);
    m_toolbarLayout->addWidget(m_closeButton);

    pm = PluginManager::instance();

    foreach (const IPluginFactory * fac, pm->getFactoryList()) {
        m_uavGadgetList->addItem(fac->classId());
    }

    if(m_plugingadget){//Load a Existing plugin
        int index = m_uavGadgetList->findText(m_plugingadget->classId());
        if(index < 0)
            index  = 0;
        m_uavGadgetList->setCurrentIndex(index);
        m_plugingadget->setParent(this);
        m_plugingadget->plugin()->setParent(this);
    }else{//Creat a new Default plugin
        m_uavGadgetList->setCurrentIndex(0);
        m_plugingadget = pm->getFactoryList().at(0)->createGadget(this);
    }

    connect(m_pagewidget, SIGNAL(currentPluginChanged(IPluginGadget*)), this, SLOT(currentPluginChanged(IPluginGadget*)));
    connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(closeView()));
    connect(m_uavGadgetList, SIGNAL(currentIndexChanged(int)), this, SLOT(doReplaceGadget(int)));

    m_layout->addLayout(m_toolbarLayout);
    m_layout->addWidget(m_plugingadget->plugin());
}

ViewWidget::~ViewWidget()
{
    if(m_plugingadget)
        delete m_plugingadget;
}

void ViewWidget::currentPluginChanged(IPluginGadget *pluginconfig)
{
    if(pluginconfig != m_plugingadget){
        if(isActive)//old focus plugin
            clearFocusPlugin();
        return;
    }
    if(isActive)//already is focused
        return;
    setFocusPlugin();
}

void ViewWidget::setFocusPlugin()
{
    QPalette pa;
    pa.setColor(QPalette::Text, Qt::red);
    m_uavGadgetList->setPalette(pa);
    pa.setColor(QPalette::ButtonText,Qt::red);
    m_closeButton->setPalette(pa);
    isActive = true;
}

void ViewWidget::clearFocusPlugin()
{
    QPalette pa;
    pa.setColor(QPalette::Text, Qt::black);
    m_uavGadgetList->setPalette(pa);
    pa.setColor(QPalette::ButtonText,Qt::black);
    m_closeButton->setPalette(pa);
    isActive = false;
}

bool ViewWidget::isFocus()
{
    return isActive;
}

int ViewWidget::indexOfClassId(QString classId)
{
    return m_uavGadgetList->findText(classId);
}

void ViewWidget::switch_plugin(int index)
{
    m_uavGadgetList->setCurrentIndex(index);
}

void ViewWidget::doReplaceGadget(int index)
{
    IPluginGadget * new_plugingadget = pm->getFactoryList().at(index)->createGadget(this);
    if(new_plugingadget){
        m_layout->removeWidget(m_plugingadget->plugin());
        delete m_plugingadget;
        m_plugingadget = new_plugingadget;
        m_layout->addWidget(m_plugingadget->plugin());
        if(isActive)
            setFocusPlugin();
        this->update();
    }
}

void ViewWidget::removePlugin()
{
    m_layout->removeWidget(m_plugingadget->plugin());
    m_plugingadget->setParent(0);
    m_plugingadget->plugin()->setParent(0);
    m_plugingadget = 0;
}

void ViewWidget::closeView()
{
    m_pagewidget->closeView(this);
}
